package com.hd.service.impl;

import com.hd.controller.dto.UserDto;
import com.hd.mapper.RoleMapper;
import com.hd.mapper.UserMapper;
import com.hd.pojo.Role;
import com.hd.pojo.User;
import com.hd.service.UserService;
import com.hd.tools.Pager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 1 on 2017/11/15.
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public User searchUserByLoginNameAndPwd(String loginName, String password) {
        return userMapper.loginByNameAndPwd(loginName,password);
    }

    @Override
    public Pager searchByCondition(Integer offset, Integer limit, UserDto dto) {
        Pager pager = new Pager();
        pager.setRows(userMapper.searchByCondition(offset,limit,dto));
        List rows = pager.getRows();
        for(Object obj : rows){
            User user = (User)obj;
            Role role = roleMapper.getRoleByRoleId(user.getRoleId());
            user.setRoleLabel(role.getRoleLabel());
        }
        pager.setTotal(userMapper.getTotal(dto));
        return pager;
    }

    @Override
    public User get(Integer userId) {
        return userMapper.get(userId);
    }

    @Override
    public void updateUserRoleId(Integer userId, Integer roleId) {
        userMapper.updateUserRoleId(userId,roleId);
    }

    @Override
    public void add(User user) {
        userMapper.add(user);
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.delete(userId);
    }

    @Override
    public List<User> getUserByRoleId(Integer roleId) {
        return userMapper.list(roleId);
    }

    @Override
    public void updateUser(User user) {
        userMapper.update(user);
    }
    @Override
    public void setPwdLiu(String loginName, String password) {
        userMapper.setPwdLiu(loginName,password);
    }

    @Override
    public List<User> searchDirector() {
        return userMapper.searchDirector();
    }

    @Override
    public void updateInLiu(String loginName, String brithday, String name, int sex, String phone, String headerimg, String address) {
        userMapper.updateInLiu(loginName,brithday,name,sex,phone,headerimg,address);
    }

    @Override
    public User getIndiv(String loginName) {
        return userMapper.getIndiv(loginName);
    }

    @Override
    public String getRole(int roleId) {
        return userMapper.getRole(roleId);
    }

}
